package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Pattern;

@RequestMapping("/emps")
@Slf4j
@RestController
public class EmpController {

    @Autowired
    EmpService empService;

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
//        log.debug("得到page： {}，pageSize：{}", page, pageSize);
        PageResult<Emp> pageResult = empService.page(empQueryParam);


        return Result.success(pageResult);
    }

    @LogOperation
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.debug("emp:{}", emp);
        empService.save(emp);
        return Result.success();
    }

    @LogOperation
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("ids:{}", ids);

        empService.delete(ids);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("id:{}", id);

        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    @LogOperation
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.debug("emp:{}", emp);
        empService.update(emp);
        return Result.success();
    }

}
